authController.$inject = ['authService', '$location'];
var $ = require('jquery');
// var swal = require('sweetalert2');

function authController(authService, $location) {

    var vm = this;


    vm.login = function () {


    // console.log(s)
        authService.login(
            vm.merchant
        ).then(function (data) {
            if (data.data == 404) {
                swal({
                    title : "Please input again",
                    text : 'name not exist or password wrong',
                    type : "error",
                    timer : 1000,
                    showConfirmButton : true,
                    confirmButtonColor : "#66CD00",
                });
                $location.path("/login");
            } else if (data.data != 500) {


                authService.getFromAdmin(data.data._id, function (response) {

                    console.log(authService.updateRealInfo(response.data));
                    var merchant=response.data;
                    try {

                        merchant._id = response.data.mid;
                        var str = JSON.stringify(merchant);
                        sessionStorage.m = str;
                    }catch(err){
                        console.log('admin server error')
                    }

                    $location.path("/info");

                }, function (response) {//admin server error
                    //add sweetalert
                    console.log('admin server error')
                })
            } else {//m side server error

            }
        });
    }


    vm.regist = function () {

        var form = document.getElementById("registForm");
        var fd = new FormData(form);
        if(fd.get('name')==''||fd.get('password')==''||fd.get('storeName')==''||fd.get('address')==''){
            swal({
                title : "Please input again",
                text : 'not allow empty field',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        if(fd.get('idcard').name ==''||fd.get('img').name==''){
            swal({
                title : "Please select again",
                text : 'not choose your file',
                type : "error",
                timer : 1000,
                showConfirmButton : true,
                confirmButtonColor : "#66CD00",
            });
            return;
        }

        // var user={"_id":"0d2defc4-2fbf-4474-9de2-3bd3f5364189","img":"http://localhost:9000/img/0d2defc4-2fbf-4474-9de2-3bd3f5364189.jpg","idcard":"http://localhost:9000/idcard/0d2defc4-2fbf-4474-9de2-3bd3f5364189.jpg","submitTime":"2017-09-01T10:13:34.246Z","block":0,"state":0,"name":"321","password":"32","storeName":"23","address":"32","__v":0};
        authService.regist(
            fd
        ).then(function (data) {

            console.log(authService.sendToAdmin(data.data));
            var socket_admin =io.connect('ws://10.222.29.179:3000')
            socket_admin.emit('new_verify', null);
            $location.path("/login");
        });
    }

    vm.toRegisterPage = function () {
        // swal("Hello world!");
        $location.path('/regist')
    }


}

module.exports = authController;